//
//  Date.swift
//  Crypto
//
//  Created by DaleGuo on 2023/9/26.
//

import Foundation

extension Date {
    
    // 一个新的构造方法 把数据的string类型事件转为Date
    init(coinGeckString:String) {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM--dd'T'HH:mm:ss.SSSZ"
        let date = formatter.date(from: coinGeckString) ?? Date()
        
        // 需要调用Date自身的init来初始化Date
        self.init(timeInterval: 0, since: date)
    }
    
    
    private var shortFormatter : DateFormatter {
        let formatter = DateFormatter()
        formatter.dateStyle = .short
        return formatter
    }
    
    func asShortDateString() -> String {
        return shortFormatter.string(from: self)
    }
}
